import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/manager', component: () => import('../views/Manager.vue'), children: [
        // 孩子作用，侧边栏不动，文本区动的一个数据渲染
        { path: 'home', meta: { title: '主页' }, component: () => import('../views/Home.vue') },  //   /manager/home
        { path: 'data', meta: { title: '数据统计' }, component: () => import('../views/Data.vue') },
        { path: 'admin', meta: { title: '管理员信息' }, component: () => import('../views/Admin.vue') },
        { path: 'person', meta: { title: '个人信息' }, component: () => import('../views/Person.vue') },
        { path: 'password', meta: { title: '修改密码' }, component: () => import('../views/Password.vue') },
        { path: 'booking', meta: { title: '酒店预订信息' }, component: () => import('../views/booking.vue') },
        { path: 'hotel', meta: { title: '酒店信息' }, component: () => import('../views/Hotel.vue') },
        { path: 'room', meta: { title: '酒店房间信息' }, component: () => import('../views/Room.vue') },
        { path: 'customer', meta: { title: '客户信息管理' }, component: () => import('../views/Customer.vue') },
      ],
    },
    {
      path: '/front',
      component: () => import('../front/FrontLayout.vue'),
      children: [
        { path: '', redirect: 'home' },
        { path: 'home', component: () => import('../front/Home.vue'), meta: { title: '前台首页' } },
        { path: 'person', component: () => import('../front/Person.vue'), meta: { title: '个人信息' } },
        { path: 'password', component: () => import('../front/Password.vue'), meta: { title: '修改密码' } },
        {
          path: 'hotel-detail/:id',
          component: () => import('../front/HotelDetail.vue'),
          meta: { title: '酒店详情' },
          name: 'HotelDetail'
        },
        {
          path: 'book-room',
          component: () => import('../front/BookRoom.vue'),
          meta: { title: '预订房间' },
          name: 'BookRoom'
        },
        {
          path: 'booking-records',
          component: () => import('../front/BookingRecords.vue'),
          meta: { title: '预订记录' },
          name: 'BookingRecords'
        },
        {
          path: 'my-reviews',
          component: () => import('../front/Review.vue'),
          meta: { title: '我的评价' },
          name: 'MyReviews'
        },
        {
          path: 'add-review/:bookingId',
          component: () => import('../front/Review.vue'),
          meta: { title: '发表评价' },
          name: 'AddReview'
        },
        {
          path: 'room-detail/:hotelId',  // 新增路由，接收 hotelId 参数
          component: () => import('../front/Room.vue'),
          meta: { title: '房间详情' }
        },
      ],
    },
    { path: '/login', name: 'Login', meta: { title: '登录系统' }, component: () => import('../views/Login.vue') },
    { path: '/register', name: 'Register', meta: { title: '欢迎注册' }, component: () => import('../views/Register.vue') },
    { path: '/404', name: 'NotFound', meta: { title: '404找不到页面' }, component: () => import('../views/404.vue') },
    { path: '/:pathMatch(.*)', redirect: '/404' },
  ],
});

// beforeEach 表示跳转之前的一些操作
// 暂时注释路由守卫，允许自由访问后台
router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  const user = JSON.parse(localStorage.getItem('xm-pro-user'));
  const isLoggedIn = !!user;
  
  // 确保统一使用ADMIN作为管理员角色标识
  const isAdmin = user && (user.role === 'ADMIN');

  console.log('路由守卫 - 用户信息:', user);
  console.log('是否登录:', isLoggedIn, '是否管理员:', isAdmin);

  // 不需要登录就可以访问的路由
  const publicPages = ['/login', '/register'];
  const requiresAuth = !publicPages.includes(to.path);

  if (!isLoggedIn && requiresAuth) {
    next('/login');
    return;
  }

  // 如果是管理员但尝试访问前台页面，重定向到管理后台
  if (isAdmin && to.path.startsWith('/front')) {
    next('/manager/home');
    return;
  }

  // 如果不是管理员但尝试访问后台页面，重定向到前台
  if (!isAdmin && to.path.startsWith('/manager')) {
    next('/front/home');
    return;
  }

  next();
});

export default router;